<?php

namespace app\models\system;

use app\models\constant\Status;
use app\models\Images;
use Yii;

/**
 * This is the model class for table "set_app".
 *
 * @property int $id
 * @property string $app_name 应用名称
 * @property string $create_time 创建日期
 * @property int $show_system 是否显示系统信息, 1显示 0关闭
 * @property int $open_test 是否开启内测, 1开启 0关闭
 * @property int $open_wx_login 是否开启微信登录, 1开启 0关闭
 * @property int $domain
 * @property int $merchant
 * @property int $referrer
 * @property int $money_qrcode 收款码
 * @property int $open_comment
 * @property int $third_pay 是否开启第三方支付
 */
class SetApp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_app';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_time'], 'safe'],
            [['show_system', 'open_test'], 'integer'],
            [['app_name'], 'string', 'max' => 255],

            [['open_wx_login'], 'safe'],

            [['domain', 'merchant', 'referrer'], 'safe'],

            // 线下支付
            [['money_qrcode'], 'safe'],
            [['only_offline_pay'], 'safe'],

            // 开启评论
            [['open_comment', 'third_pay', 'open_transfer'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'app_name' => 'App Name',
            'create_time' => 'Create Time',
            'show_system' => 'Show System',
            'open_test' => 'Open Test',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        // 收款码
        if ($model->money_qrcode) {
            $model->money_qrcode = Images::findOne(['id' => $model->money_qrcode]);
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetApp' => $data]) && $model->save()) {
            return true;
        }
    }
}
